package app.fugema.shop.main.login.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.constants.PublicGlobal;
import app.fugema.shop.http.HttpService;
import app.fugema.shop.main.login.model.MLoginBean;
import app.fugema.shop.main.login.model.MUserInfo;
import app.fugema.shop.main.login.model.OauthInfoBean;
import app.fugema.shop.main.login.model.RegisterResult;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;

import com.base.common.app.BaseConstant;
import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.viewmodel.BaseViewModel;
import com.base.common.viewmodel.NetWorkFunction;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.HashMap;
import java.util.Map;

public class AppBindVM extends BaseViewModel
{
    //第3方 登录 绑定账号    注册等
    public AppBindVM(@NonNull Application application)
    {
        super(application);
    }


    /**
     *  第三方 登录完 绑定 账户
     * @param userName
     * @param pwd
     * @return
     */
    public MutableLiveData<LiveDataWrapper<MUserInfo>> appLoginByPwd(String userName, String pwd, OauthInfoBean authInfo)
    {
        Map<String, Object> map = new HashMap<>();
        //userName  用户名
        map.put("userName",userName);
        map.put("password",pwd);
        // 注册方式二   第三方登录注册   微信 QQ 支付宝
        //oauthType   类型
        map.put("oauthType",authInfo.getOauthType());
        //OpenId
        map.put("oauthopenId",authInfo.getOauthopenId());
        //昵称
        map.put("oauthnickname",authInfo.getOauthnickname());
        if( UIUtils.isNotEmpty( authInfo.getHeadimgurl() ) )
        {
            map.put("headimgurl",authInfo.getHeadimgurl());
        }
        //变化字段
        if( UIUtils.isNotEmpty(authInfo.getUnionid()) )
        {
            map.put("unionid",authInfo.getUnionid());
        }
        map.put("sex",authInfo.getSex());
        map.put("province",authInfo.getProvince());
        map.put("city",authInfo.getCity());

        return send(HttpService.getHttpServer().appLogin(map)
                .flatMap(new NetWorkFunction<MLoginBean, ObservableSource<MUserInfo>>() {
                    @Override
                    public ObservableSource<MUserInfo> onSuccess(MLoginBean loginInfo)
                    {
                        LogUtil.e("AppBindVM:login-> flatMap()->onSuccess =  " +" UserKey = "+ loginInfo.getUserKey() +" UserI = " + loginInfo.getUserId() );
                        return userInfo(loginInfo.getUserKey(),loginInfo.getUserId());
                    }

                    @Override
                    public boolean isEmptyData(MLoginBean data) {
                        return UIUtils.isEmpty(data);
                    }

                    @Override
                    public ObservableSource<MUserInfo> onFailure(MLoginBean loginInfo)
                    {
                        //{"success":false,"msg":"用户名或密码错误","code":103}
                        MUserInfo failure =  new MUserInfo();
                        failure.setSuccess( true );
                        failure.setMsg( loginInfo.getMsg() );
                        failure.setCode( loginInfo.getCode() );
                        return getFailureObservable(failure);
                    }
                }));
    }

    /**
     * 第三方登录 信息注册
     * @param authInfo
     * @return
     */
    public MutableLiveData<LiveDataWrapper<RegisterResult>> appRegister(OauthInfoBean authInfo )
    {
        Map<String,Object> map = new HashMap<>();
        map.put("oauthType",authInfo.getOauthType());
        //OpenId
        map.put("oauthopenId",authInfo.getOauthopenId());
        //昵称
        map.put("oauthnickname",authInfo.getOauthnickname());
        if( UIUtils.isNotEmpty( authInfo.getHeadimgurl() ) )
        {
            map.put("headimgurl",authInfo.getHeadimgurl());
        }
        //变化字段
        if( UIUtils.isNotEmpty(authInfo.getUnionid()) )
        {
            map.put("unionid",authInfo.getUnionid());
        }
        map.put("sex",authInfo.getSex());
        map.put("province",authInfo.getProvince());
        map.put("city",authInfo.getCity());

        return send(HttpService.getHttpServer().registerUser(map)) ;
    }
    /**
     * 获取用户信息
     *
     * @param userKey
     * @return
     */
    public Observable<MUserInfo> userInfo(String userKey , String userId)
    {
        //保存token
        LoginUtils.saveToken(userKey, Integer.parseInt(userId));

        return HttpService.getHttpServer().userInfo(userKey)
                .map(new NetWorkFunction<MUserInfo, MUserInfo>() {
                    @Override
                    public MUserInfo onSuccess(MUserInfo userInfo)
                    {
                        //保存登录信息
                        PublicGlobal.setUserInfo(userInfo);
                        //发送用户信息刷新事件
                        LiveEventBus.get(BaseConstant.EventKey.USER_UPDATE_SUCCESS, MUserInfo.class).post(userInfo);
                        return userInfo;
                    }

                    @Override
                    public MUserInfo onFailure(MUserInfo userInfo)
                    {
                        return userInfo;
                    }
                });

    }

    /*
    public MutableLiveData<LiveDataWrapper<OauthInfoBean>> videoInfoDetail(int id) {
        return send(HttpService.getHttpServer().videoInfoDetail(id));
    }
    */

   /*
    public MutableLiveData<LiveDataWrapper<ContactResult>> videoClassroom(int current) {
        return send(HttpService.getHttpServer().videoClassroom(current, 10));
    }
    */
    /*
    public MutableLiveData<LiveDataWrapper<CommentListResult>> videoLike(int id) {
        return send(HttpService.getHttpServer().videoLike(id));
    }
    */
}
